﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using miSched.Data.Model.Core;
using System.Data.Entity;
using System.Configuration;

namespace miSched.Data.Repository.Context
{
    public class MasterContext : DbContext
    {
        public const string MASTER_CONNECTION_LITE = "MasterConnectionLite";
        public const string MASTER_CONNECTION = "MasterConnection";

        /// <summary>
        /// With custom connection string
        /// </summary>
        public MasterContext()
            : base((!string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["useSqlCe"]) && !bool.Parse(ConfigurationManager.AppSettings["useSqlCe"])) ? MASTER_CONNECTION : MASTER_CONNECTION_LITE) { }

        #region Tables
        public DbSet<Language> Languages { get; set; }
        public DbSet<Domain> Domains { get; set; }
        #endregion

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            #region Tables mapping
            modelBuilder.Entity<Domain>().Map(m =>
                {
                    m.ToTable("Domain");
                });
            modelBuilder.Entity<Language>().Map(m =>
            {
                m.ToTable("Language");
            });
            #endregion

            #region Lazy load
            modelBuilder.Entity<Language>()
                .HasMany(l => l.Domains)
                .WithRequired(d => d.Language)
                .HasForeignKey(d => d.LanguageId);
               
            #endregion
        }
    }
}
